package com.wang.designpattern.prototype;

public class Propotype implements Cloneable {

    private String id;

    private String name;

    private WorkExperience workExperience;

    public Propotype() {
    }

    public Propotype(String id, String name) {
        this.id = id;
        this.name = name;
        this.workExperience = new WorkExperience();
    }

    public Propotype(WorkExperience workExperience) {
        try {
            this.workExperience = (WorkExperience) workExperience.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public void setWorkExperience(WorkExperience workExperience) throws CloneNotSupportedException {
        this.workExperience = (WorkExperience) workExperience.clone();
    }

    public void setWorkExperience(String workDate, String company) {
        workExperience.setWorkDate(workDate);
        workExperience.setCompany(company);
    }

    public void display() {
        System.out.println("基本信息：" + id + ", " + name);
        System.out.println("工作经历：" + workExperience.getCompany() + ", " + workExperience.getWorkDate());
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Propotype propotype = (Propotype) super.clone();
        // 如果不明确用workExperience的clone来设置的话，只clone到workExperience实体的引用
        propotype.setWorkExperience(this.workExperience);
        return propotype;
    }
}

class WorkExperience implements Cloneable {

    private String workDate;

    private String company;

    public String getWorkDate() {
        return workDate;
    }

    public void setWorkDate(String workDate) {
        this.workDate = workDate;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
